package com.admin.dbdao;

import com.client.bean.SaFeAPK;
import com.data.dbdao.BaseDao;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 软件管理数据库层
 *
 * @author:HingLo
 * @create 2017-09-22 15:19
 **/
@Repository
@Transactional
public class SoftwareAdminDao extends BaseDao {
    public List<?> findSoftwareAdminDao(int type) {
        String sql = "select a.id,a.name,a.size,a.ctime ,a.uid ,(SELECT d.down FROM appdownload as d where d.id =a.id ) as down ,a.state,a.type,a.md5  from App as a where a.type=? ORDER BY down DESC";
        return super.findMysqlMethodDao(sql, type);
    }

    /***
     * 查找下线的软件信息
     * @return
     */
    public List<?> downOfflineSoftwareDao() {
        String hql = "select f.id,app.id,app.name,app.person,f.ctime ,app.type from App as app,AppOffline as f where f.rid=app.id and app.state=?";
        return super.findMassageDao(hql, 2);
    }

    /***
     * 下线软件申请
     * @param id
     * @return -2表示软件通过了下线申请
     */
    public boolean applyDownOfflineDao(String id, int state) {
        String hql = "update App set state=? where id=?";
        return super.updateOneMssageDao(hql, state, id);
    }

    /**
     * 查看评论
     *
     * @param id
     * @return
     */
    public List<?> lookSpeakingInfoDao(String id) {
        String hql = "from Speaking where rid=?";
        return super.findMassageDao(hql, id);
    }

    /***
     * 删除评论
     * @param id
     * @return
     */

    public boolean delSpeakingInfoDao(String id) {
        String hql = "delete Speaking where id=?";
        return super.updateOneMssageDao(hql, id);
    }

    public boolean delSoftwareDao(String id) {
        String hql = "delete App where id=?";
        return super.updateManyMssageDao(hql, id) > 0;
    }

    /**
     * 更新软件的状态
     *
     * @param id
     * @param state
     * @return
     */

    public boolean updateSoftwareStateDao(String id, int state) {
        String hql = "update  App set state=? where id=?";
        return updateOneMssageDao(hql, state, id);

    }

    public List<SaFeAPK> listSafeDao() {
        String hql = "from SaFeAPK";

        return (List<SaFeAPK>) super.findMassageDao(hql);
    }
}
